<template>
  <div class="result-container">
    <div class="title-wrap">
      <!-- 搜索的内容 -->
      <h2 class="title">{{$route.query.q}} (双击播放)</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              v-for="(item,index) in songList"
              :key="index"
              class="el-table__row"
              @dblclick="playMusic(item.id)"
            >
              <td>{{index+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{item.name}}</span>
                    <!-- mv的图标 -->
                    <span class="iconfont icon-mv" v-if="item.mvid!=0"></span>
                  </div>
                  <!--    //判断 二级标题有无 -->
                  <span v-if="item.alias.length!=0">{{item.alias[0]}}</span>
                </div>
              </td>
              <td>{{item.artists[0].name}}</td>
              <td>{{item.album.name}}</td>
              <td>{{item.duration}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div
            class="item"
            v-for="(item ,index) in playlists"
            :key="index"
            @click="toPlayList(item.id)"
          >
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{item.playCount}}</span>
              </div>
              <img :src="item.coverImgUrl" alt />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" v-for="(item,index) in mvs" :key="index" @click="toMv(item.id)">
            <div class="img-wrap">
              <!-- 封面 -->
              <img :src="item.cover" alt />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{item.playCount}}</div>
              </div>
              <span class="time">{{item.duration}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{item.name}}</div>
              <div class="singer">{{item.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "result",
  data() {
    return {
      activeIndex: "songs",
      //保存查询的歌曲
      songList: [],
      //搜索结果的zongshu
      count: 0,
      //切换改变数据
      activeIndex: "songs",
      //存放歌单
      playlists: [],
      //存放mv
      mvs: []
    };
  },
  watch: {
    activeIndex() {
      let type = 1;
      let limit = 10;
      switch (this.activeIndex) {
        case "songs":
          type = 1;
          limit = 10;
          break;
        case "lists":
          type = 1000;
          limit = 10;
          break;
        case "mv":
          type = 1004;
          limit = 8;
          break;
        default:
          break;
      }
      //songs 歌曲
      axios({
        url: " https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.$route.query.q,
          type,
          limit //获取的数据量
        }
      }).then(res => {
        /* console.log(res); */
        //歌曲
        if (type == 1) {
          this.songList = res.data.result.songs;
          this.count = res.data.result.songCount;
          //计算歌曲时间
          for (let i = 0; i < this.songList.length; i++) {
            //获取总毫秒数 /1000 变成秒 /60 取整数变分 剩余的秒%60
            let duration = this.songList[i].duration;
            let min = parseInt(duration / 1000 / 60);
            if (min < 10) {
              min = "0" + min;
            }
            let sec = parseInt((duration / 1000) % 60);
            if (sec < 10) {
              sec = "0" + sec;
            }
            this.songList[i].duration = `${min}:${sec}`;
          }
        } //歌单
        else if (type == 1000) {
          this.playlists = res.data.result.playlists;
          //总数
          this.count = res.data.result.playlistCount;
          //处理播放次数
          for (let i = 0; i < this.playlists.length; i++) {
            if (this.playlists[i].playCount > 100000) {
              this.playlists[i].playCount =
                parseInt(parseInt(this.playlists[i].playCount / 10000)) + "万";
            }
          }
        } //mv
        else {
          this.mvs = res.data.result.mvs;
          this.count = res.data.result.mvCount;
          //处理数据 播放次数 时间
          for (let i = 0; i < this.mvs.length; i++) {
            //获取总毫秒数 /1000 变成秒 /60 取整数变分 剩余的秒%60
            let duration = this.mvs[i].duration;
            let min = parseInt(duration / 1000 / 60);
            if (min < 10) {
              min = "0" + min;
            }
            let sec = parseInt((duration / 1000) % 60);
            if (sec < 10) {
              sec = "0" + sec;
            }
            this.mvs[i].duration = `${min}:${sec}`;
            //cishu
            if (this.mvs[i].playCount > 100000) {
              this.mvs[i].playCount =
                parseInt(parseInt(this.mvs[i].playCount / 10000)) + "万";
            }
          }
        }
      });
      //lists 歌单
      //mv
    }
  },
  created() {
    this.serach();
  },
  methods: {
    serach() {
      axios({
        url: " https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.$route.query.q,
          type: 1,
          limit: 10 //获取的数据量
        }
      }).then(res => {
        /* console.log(res); */
        this.songList = res.data.result.songs;
        this.count = res.data.result.songCount;
        //计算歌曲时间
        for (let i = 0; i < this.songList.length; i++) {
          //获取总毫秒数 /1000 变成秒 /60 取整数变分 剩余的秒%60
          let duration = this.songList[i].duration;
          let min = parseInt(duration / 1000 / 60);
          if (min < 10) {
            min = "0" + min;
          }
          let sec = parseInt((duration / 1000) % 60);
          if (sec < 10) {
            sec = "0" + sec;
          }
          this.songList[i].duration = `${min}:${sec}`;
        }
      });
    },
    playMusic(id) {
      // console.log(id)
      axios({
        url: "https://autumnfish.cn/song/url",
        method: "get",
        params: {
          id // id:id
        }
      }).then(res => {
        // console.log(res)
        let url = res.data.data[0].url;
        // console.log(this.$parent.musicUrl)
        // 设置给父组件的 播放地址
        this.$parent.musicUrl = url;
      });
    },
    //去歌单详情页 携带数据
    toPlayList(id) {
      this.$router.push("/playlist?p=" + id);
    },
    toMv(id) {
      this.$router.push(`/mv?id=${id}`);
    }
  }
};
</script>

<style >
</style>
